#include "MainMenuState.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "BitmapFont.h"
#include "GameplayState.h"
#include "Game.h"
#include "OptionsState.h"
#include "CreditsState.h"
#include "TutorialState.h"
#include "SaveState.h"
#include "CustomState.h"
#include "StaticObject.h"
#include "ParticleSystem.h"
#include "HighScore.h"
#include <fstream>

#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_Geometry.h"

MainMenuState* MainMenuState::GetInstance()
{
	static MainMenuState s_Instance;

	return &s_Instance;
}
void MainMenuState::Enter()
{

	m_nNumItems		= 0;
	m_fTimer		= 0;
	m_fSelectionTimer = .6f;
	m_bMadeSelection = false;

	std::fstream file;
	file.open("resource/menu/Menu.mnu", std::ios_base::in);
	
	if (file.is_open())
	{
		file >> m_nNumItems;
		file.ignore(INT_MAX, '\n');
		for (unsigned char i = 0; i < m_nNumItems; i++)
		{
			char szTemp[100];
			file.getline(&szTemp[0], 100, '\n');

			SGD::String strItem;
			for (unsigned char i = 0; i < 100; i++)
			{
				strItem += szTemp[i];
				if (szTemp[i] == '\0')
				{
					strItem += szTemp[i];
					break;
				}
			}
			m_szItems.push_back(strItem);
		}
	}
	file.close();

	for (unsigned int i = 0; i < m_szItems.size(); i++)
		m_mSelectedItem[i] = m_szItems.at(i).c_str();

	SGD::AudioManager* audio = SGD::AudioManager::GetInstance();

	m_hBackgroundMusic = audio->LoadAudio("resource/audio/Choose_Your_Phat.xwm");

	audio->PlayAudio(m_hBackgroundMusic, true);


	m_hBackgroundImage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/graphics/clock.png");
	
	Buttons* blueButton1 = new Buttons();
	blueButton1->LoadImage("resource/graphics/glasspill.png");
	blueButton1->LoadImage("resource/graphics/glasspill_b.png");
	blueButton1->LoadImage("resource/graphics/glasspill_cracked_b.png");
	blueButton1->SetText(m_szItems[0]);

	Buttons* blueButton2 = new Buttons();
	blueButton2->SetImage(blueButton1->GetImage(0));
	blueButton2->SetImage(blueButton1->GetImage(1));
	blueButton2->SetImage(blueButton1->GetImage(2));
	blueButton2->SetText(m_szItems[4]);

	Buttons* redButton1 = new Buttons();
	redButton1->LoadImage("resource/graphics/glasspill.png");
	redButton1->LoadImage("resource/graphics/glasspill_r.png");
	redButton1->LoadImage("resource/graphics/glasspill_cracked_r.png");
	redButton1->SetText(m_szItems[1]);

	Buttons* redButton2 = new Buttons();
	redButton2->SetImage(redButton1->GetImage(0));
	redButton2->SetImage(redButton1->GetImage(1));
	redButton2->SetImage(redButton1->GetImage(2));
	redButton2->SetText(m_szItems[5]);

	Buttons* greenButtons1 = new Buttons();
	greenButtons1->LoadImage("resource/graphics/glasspill.png");
	greenButtons1->LoadImage("resource/graphics/glasspill_g.png");
	greenButtons1->LoadImage("resource/graphics/glasspill_cracked_g.png");
	greenButtons1->SetText(m_szItems[2]);

	Buttons* greenButtons2 = new Buttons();
	greenButtons2->SetImage(greenButtons1->GetImage(0));
	greenButtons2->SetImage(greenButtons1->GetImage(1));
	greenButtons2->SetImage(greenButtons1->GetImage(2));
	greenButtons2->SetText(m_szItems[6]);

	Buttons* yellowButtons1 = new Buttons();
	yellowButtons1->LoadImage("resource/graphics/glasspill.png");
	yellowButtons1->LoadImage("resource/graphics/glasspill_y.png");
	yellowButtons1->LoadImage("resource/graphics/glasspill_cracked_y.png");
	yellowButtons1->SetText(m_szItems[3]);

	Buttons* yellowButtons2 = new Buttons();
	yellowButtons2->SetImage(yellowButtons1->GetImage(0));
	yellowButtons2->SetImage(yellowButtons1->GetImage(1));
	yellowButtons2->SetImage(yellowButtons1->GetImage(2));
	yellowButtons2->SetText(m_szItems[7]);


	blueButton1->SetPosition(SGD::Point(20, 280));
	m_vtButtons.push_back(blueButton1);
	redButton1->SetPosition(SGD::Point(20, 356));
	m_vtButtons.push_back(redButton1);
	greenButtons1->SetPosition(SGD::Point(20, 434));
	m_vtButtons.push_back(greenButtons1);
	yellowButtons1->SetPosition(SGD::Point(20, 512));
	m_vtButtons.push_back(yellowButtons1);
	blueButton2->SetPosition(SGD::Point(533, 280));
	m_vtButtons.push_back(blueButton2);
	redButton2->SetPosition(SGD::Point(533, 356));
	m_vtButtons.push_back(redButton2);
	greenButtons2->SetPosition(SGD::Point(533, 434));
	m_vtButtons.push_back(greenButtons2);
	yellowButtons2->SetPosition(SGD::Point(533, 512));
	m_vtButtons.push_back(yellowButtons2);

	m_aChange = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Change.wav");
	m_aBreak = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");
}

void MainMenuState::Exit()
{
	SGD::AudioManager::GetInstance()->UnloadAudio(m_hBackgroundMusic);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hBackgroundImage);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_aChange);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_aBreak);

	//TRUST THIS, Just let it go ************************************************************
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[0]->GetImage(0));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[0]->GetImage(1));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[0]->GetImage(2));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[1]->GetImage(0));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[1]->GetImage(1));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[1]->GetImage(2));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[2]->GetImage(0));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[2]->GetImage(1));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[2]->GetImage(2));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[3]->GetImage(0));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[3]->GetImage(1));
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_vtButtons[3]->GetImage(2));
	//***************************************************************************************

	for (unsigned int k = 0; k < m_vtButtons.size(); k++)
	{
		delete m_vtButtons[k];
	}
	
	
	m_vtButtons.clear();
	//emitterObject->GetParticle(0)->GetEmitter()->GetAliveVec().clear();
	//
	//while (emitterObject->GetParticle(0)->GetEmitter()->GetFlyweight()->GetRefCount() > 1)
	//	emitterObject->GetParticle(0)->GetEmitter()->GetFlyweight()->Release();


	//

	//delete emitterObject;
}

void MainMenuState::Update(float dt)
{
	m_fTimer += dt;
	if (m_bMadeSelection)
		m_fSelectionTimer -= dt;

	if (m_bMadeSelection)
		m_vtButtons[m_nCursor]->SetSelected(true);
	//emitterObject->GetParticle(0)->Update(dt);
	
}

void MainMenuState::Render()
{
	BitmapFont temp;
	temp.Initialize();
	Game * game =Game::GetInstance();

	SGD::GraphicsManager::GetInstance()->DrawTexture(m_hBackgroundImage, { 280, 200}, {}, {}, {}, {1,1});

	//emitterObject->GetParticle(0)->Render();

	SGD::String tittle;
	tittle = L"Time To Run!";
	temp.Draw(&tittle,(int)(game->GetScreenWidth() / 2 - 225), 40, 2.0f, SGD::Color(255,0,0));
	for (unsigned int i = 0; i < m_nNumItems; i++)
	{
		if (i == m_nCursor)
			m_vtButtons[i]->SetHighlighted(true);
		else
			m_vtButtons[i]->SetHighlighted(false);

		m_vtButtons[i]->Render();
	}

	//SGD::Point rectCenter(game->GetScreenWidth()/2 - 232.f, game->GetScreenHeight()/2 + 40.f);
	//SGD::Rectangle rect = { rectCenter, SGD::Size(410, 250) };
	//	SGD::GraphicsManager::GetInstance()->DrawRectangle(rect, SGD::Color(0, 0, 0, 0), SGD::Color(0, 255, 255), 3);
}

bool MainMenuState::Input()
{
	if (!m_bMadeSelection)
	{
		SGD::InputManager* input = SGD::InputManager::GetInstance();

		if (input->IsKeyPressed(SGD::Key::Enter) || input->IsButtonPressed(0, 0))
		{
			m_bMadeSelection = true;
			SGD::AudioManager::GetInstance()->PlayAudio(m_aBreak);
		}

		SGD::Vector vcontroller;

		if (m_fTimer > 0.25f)
		{
			vcontroller = input->GetLeftJoystick(0);
			m_fTimer = 0.f;
		}

		if (input->IsKeyPressed(SGD::Key::W)
			|| input->IsKeyPressed(SGD::Key::UpArrow)
			|| vcontroller.y < -0.2f)
		{
			--m_nCursor;
			SGD::AudioManager::GetInstance()->PlayAudio(m_aChange);
			// Stop at top
			if (m_nCursor < 0)
				m_nCursor = 0;
		}

		if (input->IsKeyPressed(SGD::Key::A)
			|| input->IsKeyPressed(SGD::Key::LeftArrow)
			|| vcontroller.x < -0.2f)
		{
			SGD::AudioManager::GetInstance()->PlayAudio(m_aChange);
			if (m_nCursor - 4 >= 0)
				m_nCursor -= 4;
		}

		if (input->IsKeyPressed(SGD::Key::D)
			|| input->IsKeyPressed(SGD::Key::RightArrow)
			|| vcontroller.x > 0.2f)
		{
			SGD::AudioManager::GetInstance()->PlayAudio(m_aChange);
			if (m_nCursor + 4 <= 7)
				m_nCursor += 4;
		}

		if (input->IsKeyPressed(SGD::Key::S)
			|| input->IsKeyPressed(SGD::Key::DownArrow)
			|| vcontroller.y > 0.2f)
		{
			++m_nCursor;
			SGD::AudioManager::GetInstance()->PlayAudio(m_aChange);
			// Stop at the bottom
			if (m_nCursor > (int)m_nNumItems - 1)
				m_nCursor = m_nNumItems - 1;
		}

		// Esc sets cursor to exit
		if (input->IsKeyPressed(SGD::Key::Escape)
			|| input->IsButtonPressed(0, 1))
			m_nCursor = m_nNumItems - 1;
	}
	else if (m_fSelectionTimer <= 0.0f)
	{
		if (m_mSelectedItem[m_nCursor] == L"New Game")
			Game::GetInstance()->ChangeState(GameplayState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Load Save")
			Game::GetInstance()->ChangeState(SaveState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Tutorial")
			Game::GetInstance()->ChangeState(GameplayState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Custom Map")
			Game::GetInstance()->ChangeState(CustomState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Options")
			Game::GetInstance()->ChangeState(OptionsState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"High Scores")
			Game::GetInstance()->ChangeState(HighScore::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Credits")
			Game::GetInstance()->ChangeState(CreditsState::GetInstance());
		if (m_mSelectedItem[m_nCursor] == L"Exit")
			return false;
	}
	return true;
}